package threadLocal;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: javase
 * @description:
 * @author: lld
 * @create: 2021-05-30 10:41
 **/
public class Demo1 {
    //    原子整数
    public static final AtomicInteger id = new AtomicInteger(0);
    //    threadlocal
    public static final ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return id.getAndIncrement();
        }
    };

    //    自定义runnable
    static class myRun implements Runnable {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ",id:" + threadLocal.get());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        myRun myRun = new myRun();
        new Thread(myRun,"a").start();
//        TimeUnit.SECONDS.sleep(1);

        new Thread(myRun,"b").start();
//        TimeUnit.SECONDS.sleep(1);

        new Thread(myRun,"c").start();
//        TimeUnit.SECONDS.sleep(1);

        new Thread(myRun,"d").start();
//        TimeUnit.SECONDS.sleep(1);
    }
}
